Skip to content

fix: raise ValueError on xp=np + jnp-backed-grid mismatch - #337

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/xp-grid-mismatch-error
May 24, 2026
Merged

fix: raise ValueError on xp=np + jnp-backed-grid mismatch#337
Jammy2211 merged 1 commit into
mainfrom
feature/xp-grid-mismatch-error

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Final PR of Phase 2 of z_features/jax_user_intro.md. Adds a clear, loud ValueError at AbstractMaker.__init__ when the caller passes xp=np but the input grid is jnp-backed (grid.use_jax=True). This is almost always a user error: someone wrote @jax.jit def f(grid): ...; return some_method(grid) and forgot to pass xp=jnp to some_method.

Today's silent failure modes:

  • NumPy ops dispatch through __array__() on the JAX tracer → host transfer (slow, "works" but secretly wrong).
  • The if xp is np: guard inside library functions wraps the result in aa.Array2D, which then fails at the JIT boundary with a confusing TypeError: ... is not a valid JAX type.

With this PR, the guard fires at the function-entry boundary with a clear message pointing at the lens_calc.py workspace guide (Phase 5d, where the JIT-it-yourself pattern is canonically documented).

API Changes

  • Changed behaviour: AbstractMaker.__init__ now raises ValueError when xp is np and grid.use_jax is True. Pre-existing call sites are unaffected — all of PyAutoArray, PyAutoLens, PyAutoGalaxy continue to pass the tests (verified: 837 / 317 / 918 pass).

See full details below.

Test Plan

  • 3 new unit tests in test_autoarray/structures/decorators/test_abstract_xp_mismatch.py:
    • Raises on xp=np + jnp-backed grid (with the expected error message format).
    • Does NOT raise on xp=np + NumPy grid (preserves existing behaviour).
    • Does NOT raise when the grid has no use_jax attribute (e.g. plain ndarray).
  • PyAutoArray 837/837 pass.
  • PyAutoLens 317/317 pass (no hidden xp/grid mismatch sites in existing code).
  • PyAutoGalaxy 918/918 pass.
Full API Changes

Changed behaviour

  • AbstractMaker.__init__(func, obj, grid, xp=np, *args, **kwargs) raises ValueError at the top of the function when xp is np and getattr(grid, "use_jax", False) is True. Error message includes the called function's qualname and a pointer to the lens_calc.py workspace guide.

Migration

  • No breaking changes for code that already passes consistent xp and grid types.
  • For users who hit the new error: add xp=jnp to the library method call inside your @jax.jit body, matching the jnp-backed grid you're passing in.

Future work

  • Opt-out kwarg (_strict_xp=False) for library internals that legitimately want the host-transfer NumPy fallback path. Not added in this PR because no current site needs it; will add if a real use case surfaces.

🤖 Generated with Claude Code

…Maker

Adds a guard at the top of AbstractMaker.__init__ that raises ValueError when
the caller declared xp=np but handed in a jnp-backed grid (grid.use_jax=True).
This is almost always a user error inside an @jax.jit body — they forgot to
pass xp=jnp to the library method.

Without this guard, the function body silently host-transfers the tracer
through __array__() (slow) or fails at the JIT boundary because the
if xp is np: guard wraps the result in an aa.Array2D. The new explicit error
points users at the lens_calc.py workspace guide for the JIT-it-yourself
pattern.

Final PR of Phase 2 of z_features/jax_user_intro.md (autoarray/simulator_use_jax.md).

Design doc: admin_jammy/notes/jax_interface.md §4.8
Issue: PyAutoArray#334

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label May 24, 2026
@Jammy2211
Jammy2211 merged commit c2ab390 into main May 24, 2026
6 checks passed
@Jammy2211
Jammy2211 deleted the feature/xp-grid-mismatch-error branch May 24, 2026 16:09
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant